<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Response对象 | 欢迎学习django课程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../part3/6.html" />
    
    
    <link rel="prev" href="../part3/4_3.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.4"
        data-chapter-title="Response对象"
        data-filepath="part3/5.md"
        data-basepath=".."
        data-revision="Tue Jan 03 2017 08:43:08 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="part1/1.html">
            
                
                    <a href="../part1/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="part1/2.html">
            
                
                    <a href="../part1/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        搭建开发环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="part1/3.html">
            
                
                    <a href="../part1/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        设计模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="part1/4.html">
            
                
                    <a href="../part1/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        管理站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="part1/5.html">
            
                
                    <a href="../part1/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="part1/6.html">
            
                
                    <a href="../part1/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="part1/7.html">
            
                
                    <a href="../part1/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="part2/1.html">
            
                
                    <a href="../part2/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模型
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="part2/2.html">
            
                
                    <a href="../part2/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        定义模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="part2/3.html">
            
                
                    <a href="../part2/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        模型成员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="part2/4.html">
            
                
                    <a href="../part2/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        模型查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="part2/5.html">
            
                
                    <a href="../part2/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="part2/6.html">
            
                
                    <a href="../part2/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="part3/1.html">
            
                
                    <a href="../part3/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="part3/2.html">
            
                
                    <a href="../part3/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        URLconf
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="part3/3.html">
            
                
                    <a href="../part3/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="part3/4.html">
            
                
                    <a href="../part3/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Request对象
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="part3/4_1.html">
            
                
                    <a href="../part3/4_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        QueryDict对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="part3/4_2.html">
            
                
                    <a href="../part3/4_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GET属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="part3/4_3.html">
            
                
                    <a href="../part3/4_3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        POST属性
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="3.4" data-path="part3/5.html">
            
                
                    <a href="../part3/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Response对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="part3/6.html">
            
                
                    <a href="../part3/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        状态保持
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="part3/7.html">
            
                
                    <a href="../part3/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="part4/1.html">
            
                
                    <a href="../part4/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="part4/2.html">
            
                
                    <a href="../part4/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        定义模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="part4/3.html">
            
                
                    <a href="../part4/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        模板继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="part4/4.html">
            
                
                    <a href="../part4/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        HTML转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="part4/5.html">
            
                
                    <a href="../part4/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        CSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="part4/6.html">
            
                
                    <a href="../part4/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="part4/7.html">
            
                
                    <a href="../part4/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="part5/1.html">
            
                
                    <a href="../part5/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="part5/2.html">
            
                
                    <a href="../part5/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        静态文件处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="part5/3.html">
            
                
                    <a href="../part5/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="part5/4.html">
            
                
                    <a href="../part5/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="part5/5.html">
            
                
                    <a href="../part5/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="part5/6.html">
            
                
                    <a href="../part5/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="part5/7.html">
            
                
                    <a href="../part5/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        ajax
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="part5/8.html">
            
                
                    <a href="../part5/8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="part6/1.html">
            
                
                    <a href="../part6/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第三方
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="part6/2.html">
            
                
                    <a href="../part6/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        富文本编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="part6/3.html">
            
                
                    <a href="../part6/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="part6/4.html">
            
                
                    <a href="../part6/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        全文检索
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="part6/5.html">
            
                
                    <a href="../part6/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        celery
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="part6/6.html">
            
                
                    <a href="../part6/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        布署
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="part6/7.html">
            
                
                    <a href="../part6/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="part7/1.html">
            
                
                    <a href="../part7/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        Git与项目
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="part7/2.html">
            
                
                    <a href="../part7/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        Git简介
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="part7/2_2.html">
            
                
                    <a href="../part7/2_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.1.</b>
                        
                        远程仓库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="part7/2_1.html">
            
                
                    <a href="../part7/2_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.2.</b>
                        
                        本地仓库
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="part7/3.html">
            
                
                    <a href="../part7/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        项目设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="part7/4.html">
            
                
                    <a href="../part7/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        模型类设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="part7/5.html">
            
                
                    <a href="../part7/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        使用页面
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="part7/6.html">
            
                
                    <a href="../part7/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >欢迎学习django课程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="httpresponse&#x5BF9;&#x8C61;">HttpResponse&#x5BF9;&#x8C61;</h1>
<ul>
<li>&#x5728;django.http&#x6A21;&#x5757;&#x4E2D;&#x5B9A;&#x4E49;&#x4E86;HttpResponse&#x5BF9;&#x8C61;&#x7684;API</li>
<li>HttpRequest&#x5BF9;&#x8C61;&#x7531;Django&#x81EA;&#x52A8;&#x521B;&#x5EFA;&#xFF0C;HttpResponse&#x5BF9;&#x8C61;&#x7531;&#x7A0B;&#x5E8F;&#x5458;&#x521B;&#x5EFA;</li>
<li>&#x4E0D;&#x8C03;&#x7528;&#x6A21;&#x677F;&#xFF0C;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x6570;&#x636E;</li>
</ul>
<pre><code>#coding=utf-8
from django.http import HttpResponse

def index(request):
    return HttpResponse(&apos;&#x4F60;&#x597D;&apos;)
</code></pre><ul>
<li>&#x8C03;&#x7528;&#x6A21;&#x677F;</li>
</ul>
<pre><code>from django.http import HttpResponse
from django.template import RequestContext, loader

def index(request):
    t1 = loader.get_template(&apos;polls/index.html&apos;)
    context = RequestContext(request, {&apos;h1&apos;: &apos;hello&apos;})
    return HttpResponse(t1.render(context))
</code></pre><h4 id="&#x5C5E;&#x6027;">&#x5C5E;&#x6027;</h4>
<ul>
<li>content&#xFF1A;&#x8868;&#x793A;&#x8FD4;&#x56DE;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;</li>
<li>charset&#xFF1A;&#x8868;&#x793A;response&#x91C7;&#x7528;&#x7684;&#x7F16;&#x7801;&#x5B57;&#x7B26;&#x96C6;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;</li>
<li>status_code&#xFF1A;&#x54CD;&#x5E94;&#x7684;HTTP&#x54CD;&#x5E94;&#x72B6;&#x6001;&#x7801;</li>
<li>content-type&#xFF1A;&#x6307;&#x5B9A;&#x8F93;&#x51FA;&#x7684;MIME&#x7C7B;&#x578B;</li>
</ul>
<h4 id="&#x65B9;&#x6CD5;">&#x65B9;&#x6CD5;</h4>
<ul>
<li><em> </em> init <em> </em>&#xFF1A;&#x4F7F;&#x7528;&#x9875;&#x5185;&#x5BB9;&#x5B9E;&#x4F8B;&#x5316;HttpResponse&#x5BF9;&#x8C61;</li>
<li>write(content)&#xFF1A;&#x4EE5;&#x6587;&#x4EF6;&#x7684;&#x65B9;&#x5F0F;&#x5199;</li>
<li>flush()&#xFF1A;&#x4EE5;&#x6587;&#x4EF6;&#x7684;&#x65B9;&#x5F0F;&#x8F93;&#x51FA;&#x7F13;&#x5B58;&#x533A;</li>
<li>set_cookie(key, value=&apos;&apos;, max_age=None, expires=None)&#xFF1A;&#x8BBE;&#x7F6E;Cookie<ul>
<li>key&#x3001;value&#x90FD;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;</li>
<li>max_age&#x662F;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x5728;&#x6307;&#x5B9A;&#x79D2;&#x6570;&#x540E;&#x8FC7;&#x671F;</li>
<li>expires&#x662F;&#x4E00;&#x4E2A;datetime&#x6216;timedelta&#x5BF9;&#x8C61;&#xFF0C;&#x4F1A;&#x8BDD;&#x5C06;&#x5728;&#x8FD9;&#x4E2A;&#x6307;&#x5B9A;&#x7684;&#x65E5;&#x671F;/&#x65F6;&#x95F4;&#x8FC7;&#x671F;&#xFF0C;&#x6CE8;&#x610F;datetime&#x548C;timedelta&#x503C;&#x53EA;&#x6709;&#x5728;&#x4F7F;&#x7528;PickleSerializer&#x65F6;&#x624D;&#x53EF;&#x5E8F;&#x5217;&#x5316;</li>
<li>max_age&#x4E0E;expires&#x4E8C;&#x9009;&#x4E00;</li>
<li>&#x5982;&#x679C;&#x4E0D;&#x6307;&#x5B9A;&#x8FC7;&#x671F;&#x65F6;&#x95F4;&#xFF0C;&#x5219;&#x4E24;&#x4E2A;&#x661F;&#x671F;&#x540E;&#x8FC7;&#x671F;</li>
</ul>
</li>
</ul>
<pre><code>from django.http import HttpResponse
from datetime import *

def index(request):
    response = HttpResponse()
    if request.COOKIES.has_key(&apos;h1&apos;):
        response.write(&apos;&lt;h1&gt;&apos; + request.COOKIES[&apos;h1&apos;] + &apos;&lt;/h1&gt;&apos;)
    response.set_cookie(&apos;h1&apos;, &apos;&#x4F60;&#x597D;&apos;, 120)
    # response.set_cookie(&apos;h1&apos;, &apos;&#x4F60;&#x597D;&apos;, None, datetime(2016, 10, 31))
    return response
</code></pre><ul>
<li>delete_cookie(key)&#xFF1A;&#x5220;&#x9664;&#x6307;&#x5B9A;&#x7684;key&#x7684;Cookie&#xFF0C;&#x5982;&#x679C;key&#x4E0D;&#x5B58;&#x5728;&#x5219;&#x4EC0;&#x4E48;&#x4E5F;&#x4E0D;&#x53D1;&#x751F;</li>
</ul>
<h4 id="&#x5B50;&#x7C7B;httpresponseredirect">&#x5B50;&#x7C7B;HttpResponseRedirect</h4>
<ul>
<li>&#x91CD;&#x5B9A;&#x5411;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8DF3;&#x8F6C;</li>
<li>&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7528;&#x6765;&#x6307;&#x5B9A;&#x91CD;&#x5B9A;&#x5411;&#x7684;&#x5730;&#x5740;</li>
</ul>
<pre><code>&#x5728;views1.py&#x4E2D;
from django.http import HttpResponse,HttpResponseRedirect
def index(request):
    return HttpResponseRedirect(&apos;js/&apos;)
def index2(request,id):
    return HttpResponse(id)

&#x5728;&#x5E94;&#x7528;&#x7684;urls.py&#x4E2D;&#x589E;&#x52A0;&#x4E00;&#x4E2A;url&#x5BF9;&#x8C61;
url(r&apos;^([0-9]+)/$&apos;, views1.index2, name=&apos;index2&apos;),
</code></pre><ul>
<li>&#x8BF7;&#x6C42;&#x5730;&#x5740;&#x680F;&#x5982;&#x56FE;&#xFF1A;</li>
</ul>
<p><img src="images/redirect1.png" alt="&#x8BF7;&#x6C42;&#x5730;&#x5740;&#x680F;"></p>
<ul>
<li>&#x8BF7;&#x6C42;&#x7ED3;&#x679C;&#x7684;&#x5730;&#x5740;&#x680F;&#x5982;&#x56FE;&#xFF1A;</li>
</ul>
<p><img src="images/redirect2.png" alt="&#x8BF7;&#x6C42;&#x5730;&#x5740;&#x680F;"></p>
<ul>
<li>&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x53CD;&#x5411;&#x89E3;&#x6790;</li>
</ul>
<pre><code>from django.core.urlresolvers import reverse

def index(request):
    return HttpResponseRedirect(reverse(&apos;booktest:index2&apos;, args=(1,)))
</code></pre><h4 id="&#x5B50;&#x7C7B;jsonresponse">&#x5B50;&#x7C7B;JsonResponse</h4>
<ul>
<li>&#x8FD4;&#x56DE;json&#x6570;&#x636E;&#xFF0C;&#x4E00;&#x822C;&#x7528;&#x4E8E;&#x5F02;&#x6B65;&#x8BF7;&#x6C42;</li>
<li><em> _init</em> _(data)</li>
<li>&#x5E2E;&#x52A9;&#x7528;&#x6237;&#x521B;&#x5EFA;JSON&#x7F16;&#x7801;&#x7684;&#x54CD;&#x5E94;</li>
<li>&#x53C2;&#x6570;data&#x662F;&#x5B57;&#x5178;&#x5BF9;&#x8C61;</li>
<li>JsonResponse&#x7684;&#x9ED8;&#x8BA4;Content-Type&#x4E3A;application/json</li>
</ul>
<pre><code>from django.http import JsonResponse

def index2(requeset):
    return JsonResponse({&apos;list&apos;: &apos;abc&apos;})
</code></pre><h1 id="&#x7B80;&#x5199;&#x51FD;&#x6570;">&#x7B80;&#x5199;&#x51FD;&#x6570;</h1>
<h4 id="render">render</h4>
<ul>
<li>render(request, template_name[, context])</li>
<li>&#x7ED3;&#x5408;&#x4E00;&#x4E2A;&#x7ED9;&#x5B9A;&#x7684;&#x6A21;&#x677F;&#x548C;&#x4E00;&#x4E2A;&#x7ED9;&#x5B9A;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x5B57;&#x5178;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6E32;&#x67D3;&#x540E;&#x7684;HttpResponse&#x5BF9;&#x8C61;</li>
<li>request&#xFF1A;&#x8BE5;request&#x7528;&#x4E8E;&#x751F;&#x6210;response</li>
<li>template_name&#xFF1A;&#x8981;&#x4F7F;&#x7528;&#x7684;&#x6A21;&#x677F;&#x7684;&#x5B8C;&#x6574;&#x540D;&#x79F0;</li>
<li>context&#xFF1A;&#x6DFB;&#x52A0;&#x5230;&#x6A21;&#x677F;&#x4E0A;&#x4E0B;&#x6587;&#x7684;&#x4E00;&#x4E2A;&#x5B57;&#x5178;&#xFF0C;&#x89C6;&#x56FE;&#x5C06;&#x5728;&#x6E32;&#x67D3;&#x6A21;&#x677F;&#x4E4B;&#x524D;&#x8C03;&#x7528;&#x5B83;</li>
</ul>
<pre><code>from django.shortcuts import render

def index(request):
    return render(request, &apos;booktest/index.html&apos;, {&apos;h1&apos;: &apos;hello&apos;})
</code></pre><h4 id="&#x91CD;&#x5B9A;&#x5411;">&#x91CD;&#x5B9A;&#x5411;</h4>
<ul>
<li>redirect(to)</li>
<li>&#x4E3A;&#x4F20;&#x9012;&#x8FDB;&#x6765;&#x7684;&#x53C2;&#x6570;&#x8FD4;&#x56DE;HttpResponseRedirect</li>
<li>to&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x53CD;&#x5411;&#x89E3;&#x6790;</li>
</ul>
<pre><code>from django.shortcuts import redirect
from django.core.urlresolvers import reverse

def index(request):
    return redirect(reverse(&apos;booktest:index2&apos;))
</code></pre><h4 id="&#x5F97;&#x5230;&#x5BF9;&#x8C61;&#x6216;&#x8FD4;&#x56DE;404">&#x5F97;&#x5230;&#x5BF9;&#x8C61;&#x6216;&#x8FD4;&#x56DE;404</h4>
<ul>
<li>get_object_or_404(klass, <em>args, *</em>kwargs)</li>
<li>&#x901A;&#x8FC7;&#x6A21;&#x578B;&#x7BA1;&#x7406;&#x5668;&#x6216;&#x67E5;&#x8BE2;&#x96C6;&#x8C03;&#x7528;get()&#x65B9;&#x6CD5;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x627E;&#x5230;&#x5BF9;&#x8C61;&#xFF0C;&#x4E0D;&#x5F15;&#x53D1;&#x6A21;&#x578B;&#x7684;DoesNotExist&#x5F02;&#x5E38;&#xFF0C;&#x800C;&#x662F;&#x5F15;&#x53D1;Http404&#x5F02;&#x5E38;</li>
<li>klass&#xFF1A;&#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x7684;&#x6A21;&#x578B;&#x7C7B;&#x3001;Manager&#x5BF9;&#x8C61;&#x6216;QuerySet&#x5BF9;&#x8C61;</li>
<li>**kwargs&#xFF1A;&#x67E5;&#x8BE2;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x683C;&#x5F0F;&#x5E94;&#x8BE5;&#x53EF;&#x4EE5;&#x88AB;get()&#x548C;filter()&#x63A5;&#x53D7;</li>
<li>&#x5982;&#x679C;&#x627E;&#x5230;&#x591A;&#x4E2A;&#x5BF9;&#x8C61;&#x5C06;&#x5F15;&#x53D1;MultipleObjectsReturned&#x5F02;&#x5E38;</li>
</ul>
<pre><code>from django.shortcuts import *

def detail(request, id):
    try:
        book = get_object_or_404(BookInfo, pk=id)
    except BookInfo.MultipleObjectsReturned:
        book = None
    return render(request, &apos;booktest/detail.html&apos;, {&apos;book&apos;: book})

&#x5C06;settings.py&#x4E2D;&#x7684;DEBUG&#x6539;&#x4E3A;False
&#x5C06;&#x8BF7;&#x6C42;&#x5730;&#x5740;&#x8F93;&#x5165;2&#x548C;100&#x67E5;&#x770B;&#x6548;&#x679C;
</code></pre><h4 id="&#x5F97;&#x5230;&#x5217;&#x8868;&#x6216;&#x8FD4;&#x56DE;404">&#x5F97;&#x5230;&#x5217;&#x8868;&#x6216;&#x8FD4;&#x56DE;404</h4>
<ul>
<li>get_list_or_404(klass, <em>args, *</em>kwargs)</li>
<li>klass&#xFF1A;&#x83B7;&#x53D6;&#x5217;&#x8868;&#x7684;&#x4E00;&#x4E2A;Model&#x3001;Manager&#x6216;QuerySet&#x5B9E;&#x4F8B;</li>
<li>**kwargs&#xFF1A;&#x67E5;&#x5BFB;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x683C;&#x5F0F;&#x5E94;&#x8BE5;&#x53EF;&#x4EE5;&#x88AB;get()&#x548C;filter()&#x63A5;&#x53D7;</li>
</ul>
<pre><code>from django.shortcuts import *

def index(request):
    # list = get_list_or_404(BookInfo, pk__lt=1)
    list = get_list_or_404(BookInfo, pk__lt=6)
    return render(request, &apos;booktest/index.html&apos;, {&apos;list&apos;: list})

&#x5C06;settings.py&#x4E2D;&#x7684;DEBUG&#x6539;&#x4E3A;False
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../part3/4_3.html" class="navigation navigation-prev " aria-label="Previous page: POST属性"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../part3/6.html" class="navigation navigation-next " aria-label="Next page: 状态保持"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
